package com.neusoft.a132wxyorderingdish.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.beans.ResultBean;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.ShopModel;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class AlterUserInfoActivity extends BaseActivity implements RetrofitListener<ResultBean> {
    private TextView textView5;
    private TextView textView7;
    private TextView textView9;
    private TextView textView12;
    private TextView textView13;
    private ImageButton imageButton,alter_user_photo;
    private Button alter_sumbit;
    private EditText alter_username,alter_pass,alter_password,alter_phone,alter_address;
    String srcPath="";
    @Override
    void initViews() {
        setLayout_file(R.layout.activity_alter_user_info);
        imageButton=findViewById(R.id.alter_back);
        textView5=findViewById(R.id.textView5);
        textView7=findViewById(R.id.textView7);
        textView9=findViewById(R.id.textView9);
        textView12=findViewById(R.id.textView12);
        textView13=findViewById(R.id.textView13);
        alter_sumbit=findViewById(R.id.alter_sumbit);
        alter_username=findViewById(R.id.alter_username);
        alter_pass=findViewById(R.id.alter_pass);
        alter_password=findViewById(R.id.alter_password);
        alter_phone=findViewById(R.id.alter_phone);
        alter_address=findViewById(R.id.alter_address);
        alter_user_photo=findViewById(R.id.alter_user_photo);
    }

    @Override
    void initEvents() {
        alter_sumbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commit();
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlterUserInfoActivity.this.finish();
            }
        });

        alter_user_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(AlterUserInfoActivity.this,PhotoActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    void initData() {
        Intent intent=getIntent();
        alter_username.setText(getIntent().getStringExtra("username"));
        alter_pass.setText(getUser_password());
        alter_phone.setText(getIntent().getStringExtra("address"));
        alter_address.setText(getIntent().getStringExtra("phone"));

        SharedPreferences sp=getSharedPreferences("userphoto",MODE_PRIVATE);
        srcPath=sp.getString("srcPath","");
        if(!"".equals(srcPath)){
            setFileToImage();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onSuccess(ResultBean resultBean, int flag) {


        if(resultBean.getSuccess().equals("1")){
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onFail() {

    }
    private void commit(){
        //保存图片路径
        SharedPreferences sp=getSharedPreferences("userphoto",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("srcPath",srcPath);
        editor.commit();

        ShopModel shopModel=new ShopModel();
        shopModel.updateUserById(getUser_id(),
                alter_username.getText().toString(),
                alter_password.getText().toString(),
                alter_phone.getText().toString(),
                alter_address.getText().toString(),this);
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1){
            Bundle bundle=data.getExtras();
            srcPath=bundle.getString("srcPath");
            if(!srcPath.equals("")){
                setFileToImage();
            }
        }
    }

    void setFileToImage(){
        try{
            FileInputStream f=new FileInputStream(srcPath);
            Bitmap bm=null;
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize=8;
            BufferedInputStream bis=new BufferedInputStream(f);
            bm=BitmapFactory.decodeStream(bis,null,options);
            alter_user_photo.setImageBitmap(bm);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}